<?php

namespace app\repositories\eloquent;
use app\repositories\contracts\RootGroupInterface;
use app\admin\model\AdminRootGroup;
use think\response\Json;
use function foo\func;

/***
 * Class RootGroupProvider
 * @package app\repositories\eloquent
 * @Author Vee7
 * @Date 2021/12/14
 * @Time 13:11
 * @Description 实现后台会员组接口
 */
class RootGroupProvider implements RootGroupInterface
{
    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取会员组分页列表
     */
    public function getRootGroupListPage($page,$limit,$orderby='id',$order='asc'):array
    {
        return AdminRootGroup::limit((($page-1)*$limit),$limit)->order($orderby, $order)->select()->toArray();
    }

    /***
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取会员组总数据量
     */
    public function getRootGroupListCount():int
    {
        return AdminRootGroup::count();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/14
     * @Time 13:12
     * @Description 从模型获取会员组列表
     */
    public function getRootGroupList($orderby='id',$order='asc'):array
    {
        return AdminRootGroup::order($orderby, $order)->select()->toArray();
    }

    /***
     * @return array
     * @Author Vee7
     * @Date 2021/12/16
     * @Time 10:22
     * @Description 获取指定id的会员组数据
     */
    public function getRootGroupData($id):array
    {
        return AdminRootGroup::find($id)->toArray();
    }

    /***
     * @param array $id
     * @return bool
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 9:23
     * @Description 按ID删除会员组
     */
    public function delRootGroupList(array $id):bool
    {
        $list = AdminRootGroup::select($id);
        $result = $list->delete();
        return $result;
    }

    /***
     * @param array $data
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 10:43
     * @Description 根据指定参数批量修改会员组字段值
     *
     */
    public function editRootGroupList(array $data)
    {
        //前端应传递如下格式的二维数组
        //var editData = [{'id':id,'key':key,'value':value},{...}];
        $RootGroup_obj = new AdminRootGroup;
        foreach ($data as $k=>$v){
            foreach ($v as $key=>$value){
                $list[] = $value;
            }
        }
        //返回修改后的数据
        return $RootGroup_obj->saveAll($list)->toArray();
    }

    /***
     * @param array $addArray
     * @Author Vee7
     * @Date 2021/12/15
     * @Time 14:48
     * @Description 新增会员组
     */
    public function addRootGroupList(array $addArray)
    {
        $RootGroup_obj = new AdminRootGroup;
        $list = [];
        foreach ($addArray as $v){
            //saveAll函数无法自动添加时间戳，手动添加
            $v['create_time'] = date("Y-m-d H:i:s ");
            $v['allow_menu_id'] = "1,2";
            $list[] = $v;
        }
        return $RootGroup_obj->saveAll($list)->toArray();
    }

}